package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.vo.PageBean;

public class FamilyDao extends GenericDao<Family>{
	@Override
	public Family handle(ResultSet rs) throws SQLException {	
		Family f =new Family();
		f.setFamilycode(rs.getString("familycode"));
		f.setFamilyattribute(rs.getString("familyattribute"));
		f.setFamilynum(rs.getInt("familynum"));
		f.setFarmernum(rs.getInt("farmernum"));
		f.setTown(rs.getString("town"));
		f.setVillage(rs.getString("village"));
		f.setGroup(rs.getString("group"));
		f.setAddress(rs.getString("address"));
		f.setBuildtime(rs.getTimestamp("buildtime"));
		f.setUsername(rs.getString("username"));
		return f;
	}
	
	
	
	public void add(Family f)throws SQLException, ParseException{
		Connection conn=null;
		PreparedStatement pstmt=null;
		try {
			DbUtil db = new DbUtil();
			conn=db.getConn();
			conn.setAutoCommit(false);
			String sql = "insert into family(familycode,familyattribute,familynum,farmernum,town,village,`group`,address,buildtime,username) values(?,?,?,?,?,?,?,?,?,?)";
			pstmt=conn.prepareStatement(sql);
			int index=1;
			pstmt.setString(index++, f.getFamilycode());
			pstmt.setString(index++, f.getFamilyattribute());
			pstmt.setInt(index++, f.getFamilynum());
			pstmt.setInt(index++, f.getFarmernum());
			pstmt.setString(index++, f.getTown());
			pstmt.setString(index++, f.getVillage());
			pstmt.setString(index++, f.getGroup());
			pstmt.setString(index++, f.getAddress());
			pstmt.setTimestamp(index++, new java.sql.Timestamp(new Date().getTime()));
			pstmt.setString(index++, f.getUsername());
			pstmt.executeUpdate();
			f.getHolder().setFamily(f);
			f.getHolder().add(conn);//此处已commit
		} catch (SQLException e) {
			throw new SQLException("添加参合家庭档案失败 " + e.getMessage(), e);
		}finally{
			pstmt.close();
			conn.close();
		}
	}
	public void del(String familycode)throws SQLException{
		try {
			DbUtil db = new DbUtil();
			String sql = "delete from family where familycode='" + familycode + "'";
			db.executeUpdate(sql);
		} catch (SQLException e) {
			throw new SQLException("删除参合家庭档案失败" + e.getMessage(), e);
		}
	}
	public Family query(String familycode)throws SQLException{
		String sql ="select * from family where familycode=?";
		Object[] params = {familycode};
		List<Family> list = new ArrayList<Family>();
		list = super.queryAll(sql, params);
		if(list.size()!=0)
			return list.get(0);
		else
			return null;
	}	
	public List<Family> queryAll()throws SQLException{
		String sql ="select * from family";
		return super.queryAll(sql, null);
	}
	public PageBean queryAll(int page,int size)throws SQLException{
		String sql ="select * from family";
		return super.queryAll(sql, null, page, size);
	}
	public void update(Family f)throws SQLException{
		Connection conn=null;
		PreparedStatement pstmt=null;
		try {
			DbUtil db = new DbUtil();
			conn=db.getConn();
			String sql = "update family set familyattribute=?,town=?,village=?,`group`=?,address=? where familycode=?";
			pstmt=conn.prepareStatement(sql);
			int index=1;
			pstmt.setString(index++, f.getFamilyattribute());
			pstmt.setString(index++, f.getTown());
			pstmt.setString(index++, f.getVillage());
			pstmt.setString(index++, f.getGroup());
			pstmt.setString(index++, f.getAddress());
			pstmt.setString(index++, f.getFamilycode());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("修改参合家庭档案失败 " + e.getMessage(), e);
		}
		finally{
			pstmt.close();
			conn.close();
		}
	}

}
